package cn.tedu.fruitshop.controller;

import cn.tedu.fruitshop.pojo.dto.MakerAddNewDTO;
import cn.tedu.fruitshop.pojo.dto.MakerUpdateDTO;
import cn.tedu.fruitshop.pojo.vo.MakerListItemVO;
import cn.tedu.fruitshop.service.IMakerService;
import cn.tedu.fruitshop.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@Validated
@RestController
@RequestMapping("/makers")
@Api(tags = "04. 店铺管理模块")
public class MakerController {
    @Autowired
    private IMakerService makerService;

    public MakerController() {
        log.debug("创建控制器对象：MakerController");
    }

    // http://localhost:8080/makers/add-new?name=店铺001&description=店铺001的简介&sort=199
    @ApiOperation("添加店铺")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(@Valid MakerAddNewDTO makerAddNewDTO) {
        log.debug("开始处理【添加店铺】的请求，参数：{}", makerAddNewDTO);
        makerService.addNew(makerAddNewDTO);
        log.debug("添加店铺成功！");
        return JsonResult.ok();
    }

    // http://localhost:8080/makers/9527/delete
    @ApiOperation("根据id删除店铺")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "店铺id", required = true, dataType = "Integer")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@Range(min = 1, message = "删除店铺失败，尝试删除的店铺的ID无效！")
                                   @PathVariable Integer id) {
        log.debug("开始处理【根据id删除店铺】的请求，参数：{}", id);
        makerService.delete(id);
        return JsonResult.ok();
    }

    // http://localhost:8080/makers
    @ApiOperation("查询店铺列表")
    @ApiOperationSupport(order = 420)
    @GetMapping("")
    public JsonResult<List<MakerListItemVO>> list() {
        log.debug("开始处理【查询店铺列表】的请求，无参数");
        List<MakerListItemVO> list = makerService.list();
        return JsonResult.ok(list);
    }
    @ApiOperation("修改店铺详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "店铺id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult<Void> updateById(@PathVariable int id, MakerUpdateDTO makerUpdateDTO) {
        log.debug("开始处理【修改店铺详情】的请求，参数ID：{}, 新数据：{}", id, makerUpdateDTO);
        makerService.updateInfoById(id, makerUpdateDTO);
        return JsonResult.ok();
    }
}
